Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle ast.parse failures when converting function type comments #616

Merged
merged 2 commits into from
Jan 21, 2022

Conversation

stroxler
Copy link
Contributor

Summary

We have to parse code using the ast module in two situations, and in either case we can hit parse errors even though the code is valid python.

(a) by parsing with type_comments=True to reliably extract string type comments from code. This can throw errors if there are comments of the form # type: something anywhere where type information is invalid according to PEP 484.

(b) by parsing the type comments themselves into expressions or ast.FunctionType values. This can throw errors, even if (a) succeeds, whenever the type itself is not well-formed.

This commit updates convert_type_comments to handle these situations by ignoring type information on a specific node - prior to this commit, the entire codemod would have crashed, but there may be many other type comments in the module which are perfectly valid and we'd like to be able to keep going when we hit SyntaxErrors.

Test Plan

I added new test cases for invalid type comments

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 20, 2022
If we encounter a syntax error in either the type comment extraction
or the type comment parsing stages, ignore type information on that
cst node.
@codecov-commenter
Copy link

codecov-commenter commented Jan 20, 2022

Codecov Report

Merging #616 (1d620da) into main (ceac7fc) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #616   +/-   ##
=======================================
  Coverage   94.78%   94.78%           
=======================================
  Files         245      245           
  Lines       25238    25250   +12     
=======================================
+ Hits        23921    23934   +13     
+ Misses       1317     1316    -1     
Impacted Files Coverage Δ
libcst/codemod/commands/convert_type_comments.py 95.98% <100.00%> (+0.69%) ⬆️
...demod/commands/tests/test_convert_type_comments.py 96.72% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ceac7fc...1d620da. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants